package com.z.realm;


import com.z.entity.Perms;
import com.z.entity.Role;
import com.z.entity.User;
import com.z.service.RegisterService;
import com.z.utils.ApplicationContextUtils;
import org.apache.shiro.authc.AuthenticationException;
import org.apache.shiro.authc.AuthenticationInfo;
import org.apache.shiro.authc.AuthenticationToken;
import org.apache.shiro.authc.SimpleAuthenticationInfo;
import org.apache.shiro.authz.AuthorizationInfo;
import org.apache.shiro.authz.SimpleAuthorizationInfo;
import org.apache.shiro.realm.AuthorizingRealm;
import org.apache.shiro.subject.PrincipalCollection;
import org.apache.shiro.util.ByteSource;
import org.apache.shiro.util.CollectionUtils;
import org.springframework.util.ObjectUtils;

import java.util.HashMap;
import java.util.List;

/**
 * @ProjectName: springboot-08-shiro
 * @Package: com.baizhi.shiro.realm
 * @ClassName: CustomerRealm
 * @author: Arisapoke
 * @date: 2021/2/3 10:56
 * @Version: 1.0
 */
public class CustomerRealm extends AuthorizingRealm {

    @Override
    protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principals) {

        //1 获取bean实例对象
        RegisterService registerService = (RegisterService) ApplicationContextUtils.getBean("registerService");
        //获取身份信息
        String primaryPrincipal = (String) principals.getPrimaryPrincipal();

        System.out.println("调用授权信息" + primaryPrincipal);

        List<Role> roles = registerService.selectAllRoles(primaryPrincipal);
        //赋值授权角色信息
        if (!CollectionUtils.isEmpty(roles)) {
            SimpleAuthorizationInfo simpleAuthorizationInfo = new SimpleAuthorizationInfo();
            roles.forEach(role -> {
                simpleAuthorizationInfo.addRole(role.getName());

                //权限信息
                List<Perms> perms = registerService.findPermsByRoleId(role.getId());
                if (!CollectionUtils.isEmpty(perms)){
                    //遍历添加权限信息
                    perms.forEach(perm -> {
                        simpleAuthorizationInfo.addStringPermission(perm.getName());
                    });
                }

            });
            return simpleAuthorizationInfo;
        }


        return null;
    }

    @Override
    protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken token) throws AuthenticationException {
//        SimpleAuthenticationInfo simpleAuthenticationInfo = new SimpleAuthenticationInfo();
        System.out.println("=====realm=====");
        //获得用户信息
        String principal = (String) token.getPrincipal();
        //1 获取bean实例对象
        RegisterService registerService = (RegisterService) ApplicationContextUtils.getBean("registerService");
//        //2 创建一个HashMap用以存储查询条件 查询对象参数
//        HashMap<String, Object> map = new HashMap<>();
//        //3 设置查询条件
//        map.put("username",principal);
        //4 调用查询方法
        User user = registerService.login(principal);


        if (!ObjectUtils.isEmpty(user)) {
            return new SimpleAuthenticationInfo(user.getUsername(), user.getPassword(), ByteSource.Util.bytes(user.getSalt()), this.getName());
        }


        return null;
    }
}
